package cn.wgoodq.study.MultiThreading.exercise;

import cn.wgoodq.study.MultiThreading.exercise.entity.Printer;

/**
 * Created by OuKyou on 2017/1/21.
 */
public class ThreadsCommunication {

    public static void main(String[] args) {
        try {
            Printer printer = new Printer();
            Runnable1 runnable1 = new Runnable1(printer);
            Thread thread1 = new Thread(runnable1);
            thread1.start();

            Runnable2 runnable2 = new Runnable2(printer);
            Thread thread2 = new Thread(runnable2);
            thread2.start();

            System.out.println("main end");

        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    static class Runnable1 implements Runnable {
        private Printer printer;

        Runnable1(Printer printer) {
            this.printer = printer;
        }

        @Override
        public void run() {
            try {
                for (int i = 65; i < 91; i++) {
                    printer.printCharacter(i);
                }
                System.out.println("Runnable1 end");
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

    static class Runnable2 implements Runnable {
        private Printer printer;

        Runnable2(Printer printer) {
            this.printer = printer;
        }

        @Override
        public void run() {
            try {
                for (int i = 0; i < 54; i++) {
                    i = printer.printFigure(i);
                }
                System.out.println("Runnable2 end");
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}
